//
//  UIView+Category.m
//  dudu_oc_master
//
//  Created by mac on 2019/11/11.
//  Copyright © 2019 Shawn Ma. All rights reserved.
//

#import "UIView+Category.h"

@implementation UIView (Category)


- (void)setTj_x:(CGFloat)tj_x {
    CGRect frame = self.frame;
    frame.origin.x = tj_x;
    self.frame = frame;
}

- (CGFloat)tj_x {
    return self.frame.origin.x;
}

- (void)setTj_y:(CGFloat)tj_y {
    CGRect frame = self.frame;
    frame.origin.y = tj_y;
    self.frame = frame;
}

- (CGFloat)tj_y {
    return self.frame.origin.y;
}

- (void)setTj_width:(CGFloat)tj_width {
    CGRect frame = self.frame;
    frame.size.width = tj_width;
    self.frame = frame;
}

- (CGFloat)tj_width {
    return self.frame.size.width;
}

- (void)setTj_height:(CGFloat)tj_height {
    CGRect frame = self.frame;
    frame.size.height = tj_height;
    self.frame = frame;
}

- (CGFloat)tj_height {
    return self.frame.size.height;
}

- (void)setTj_size:(CGSize)tj_size {
    CGRect frame = self.frame;
    frame.size = tj_size;
    self.frame = frame;
}

- (CGSize)tj_size {
    return self.frame.size;
}

- (void)setTj_centerX:(CGFloat)tj_centerX {
    CGPoint center = self.center;
    center.x = tj_centerX;
    self.center = center;
}

- (CGFloat)tj_centerX {
    return self.center.x;
}

- (void)setTj_centerY:(CGFloat)tj_centerY {
    CGPoint center = self.center;
    center.y = tj_centerY;
    self.center = center;
}


- (CGFloat)tj_top {
    return self.frame.origin.y;
}

- (void)setTj_top:(CGFloat)tj_top {
    CGRect frame = self.frame;
    frame.origin.y = tj_top;
    self.frame = frame;
}

- (CGFloat)tj_bottom {
    return self.frame.origin.y + self.frame.size.height;
}
 
- (CGFloat)tj_centerY {
    return self.center.y;
}

- (CGFloat)tj_MaxY {
    return CGRectGetMaxY(self.frame);
}

- (CGFloat)tj_MaxX {
    return CGRectGetMaxX(self.frame);
}


+ (_Nonnull instancetype)tj_addToView:(nonnull UIView *)superView {
    UIView *view = [[self alloc] init];
    [superView addSubview:view];
    return view;
}

+ (_Nonnull instancetype)buttonWithSuperView:(UIView *)superView font:(UIFont *)font color:(nullable UIColor *)color title:(nullable NSString *)title {
    UIButton *button = [[self alloc] init];
    if (superView) [superView addSubview:button];
    [button setTitle:title forState:UIControlStateNormal];
    if (color) [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = font;
    return button;
}

+ (_Nonnull instancetype)lableWithSuperView:(UIView *)superView font:(UIFont *)font color:(nullable UIColor *)color title:(nullable NSString *)title textAlignment:(NSTextAlignment)textAlignment {
    UILabel *label = [[self alloc] init];
    label.userInteractionEnabled = YES;
    if (superView) [superView addSubview:label];
    label.text = title;
    if (color) label.textColor = color;
    label.font = font;
    label.textAlignment = textAlignment;
    return label;
}

- (nonnull UIView *)shearRoundedCornersWithRadiu:(CGFloat)radiu {
    self.layer.cornerRadius = radiu;
    self.layer.masksToBounds = YES;
    return self;
}

- (void)addTapGesturesWithTarget:(nullable id)id action:(nonnull SEL)action {
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:id action:action]];
}

- (void)drawRoundedWithByRoundingCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    shape.frame = self.bounds;
    [shape setPath:path.CGPath];
    self.layer.mask = shape;
}

- (void) addRoundedLine{
    self.layer.cornerRadius = 2;
     self.layer.borderWidth = 0.5;
     self.layer.borderColor = [[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] CGColor];
}

+ (instancetype)viewFromXib
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
}
@end
